"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.types = exports["default"] = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _utils = require("./utils");

var _lodash = require("lodash");

var types = _interopRequireWildcard(require("./types"));

exports.types = types;

var Sdk = /*#__PURE__*/function () {
  function Sdk(_conf) {
    var _this = this;

    (0, _classCallCheck2["default"])(this, Sdk);
    (0, _defineProperty2["default"])(this, "worker", void 0);
    (0, _defineProperty2["default"])(this, "end", 'end');
    (0, _defineProperty2["default"])(this, "defaultTimeout", void 0);
    (0, _defineProperty2["default"])(this, "open", function (conf) {
      var workerPath = conf.workerPath,
          mediaType = conf.mediaType,
          onSuccess = conf.onSuccess,
          onFail = conf.onFail;
      (0, _utils.setMediaType)(mediaType);
      var worker = (0, _utils.createWorker)(workerPath);
      var p1 = (0, _utils.waitForWorkerIsReady)(worker, onSuccess, onFail);
      var p2 = (0, _utils.createTimeoutPromise)(30 * 1000);
      _this.worker = worker;
      return Promise.race([p1, p2]);
    });
    (0, _defineProperty2["default"])(this, "close", function () {
      _this.worker.terminate();

      _this.worker = null;
    });
    (0, _defineProperty2["default"])(this, "innerSplice", /*#__PURE__*/function () {
      var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(originBlob, startSecond, endSecond, insertBlob) {
        var ss, es, logs, originAb, leftSideArrBuf, rightSideArrBuf, result, _result, result1, result2, arrBufs, combindResult;

        return _regenerator["default"].wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                ss = startSecond;
                es = (0, _lodash.isNumber)(endSecond) ? endSecond : _this.end;
                logs = [];
                insertBlob = insertBlob ? insertBlob : endSecond && !(0, _lodash.isNumber)(endSecond) ? endSecond : null;
                _context.next = 6;
                return (0, _utils.blobToArrayBuffer)(originBlob);

              case 6:
                originAb = _context.sent;

                if (!(ss === 0 && es === _this.end)) {
                  _context.next = 11;
                  break;
                }

                return _context.abrupt("return", {
                  blob: insertBlob,
                  logs: []
                } || null);

              case 11:
                if (!(ss === 0)) {
                  _context.next = 19;
                  break;
                }

                _context.next = 14;
                return (0, _utils.pmToPromise)(_this.worker, (0, _utils.getClipCommand)(originAb, es));

              case 14:
                result = _context.sent;
                rightSideArrBuf = result.buffer;
                logs.push(result.logs);
                _context.next = 37;
                break;

              case 19:
                if (!(ss !== 0 && es === _this.end)) {
                  _context.next = 27;
                  break;
                }

                _context.next = 22;
                return (0, _utils.pmToPromise)(_this.worker, (0, _utils.getClipCommand)(originAb, 0, ss));

              case 22:
                _result = _context.sent;
                leftSideArrBuf = _result.buffer;
                logs.push(_result.logs);
                _context.next = 37;
                break;

              case 27:
                _context.next = 29;
                return (0, _utils.pmToPromise)(_this.worker, (0, _utils.getClipCommand)(originAb, 0, ss));

              case 29:
                result1 = _context.sent;
                leftSideArrBuf = result1.buffer;
                logs.push(result1.logs);
                _context.next = 34;
                return (0, _utils.pmToPromise)(_this.worker, (0, _utils.getClipCommand)(originAb, es));

              case 34:
                result2 = _context.sent;
                rightSideArrBuf = result2.buffer;
                logs.push(result2.logs);

              case 37:
                arrBufs = [];
                leftSideArrBuf && arrBufs.push(leftSideArrBuf);
                _context.t0 = insertBlob;

                if (!_context.t0) {
                  _context.next = 46;
                  break;
                }

                _context.t1 = arrBufs;
                _context.next = 44;
                return (0, _utils.blobToArrayBuffer)(insertBlob);

              case 44:
                _context.t2 = _context.sent;

                _context.t1.push.call(_context.t1, _context.t2);

              case 46:
                rightSideArrBuf && arrBufs.push(rightSideArrBuf);
                _context.t3 = _utils.pmToPromise;
                _context.t4 = _this.worker;
                _context.next = 51;
                return (0, _utils.getCombineCommand)(arrBufs);

              case 51:
                _context.t5 = _context.sent;
                _context.next = 54;
                return (0, _context.t3)(_context.t4, _context.t5);

              case 54:
                combindResult = _context.sent;
                logs.push(combindResult.logs);
                return _context.abrupt("return", {
                  blob: (0, _utils.audioBufferToBlob)(combindResult.buffer),
                  logs: logs
                });

              case 57:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }));

      return function (_x, _x2, _x3, _x4) {
        return _ref.apply(this, arguments);
      };
    }());
    (0, _defineProperty2["default"])(this, "splice", /*#__PURE__*/function () {
      var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(originBlob, startSecond, endSecond, insertBlob) {
        return _regenerator["default"].wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                return _context2.abrupt("return", Promise.race([_this.innerSplice(originBlob, startSecond, endSecond, insertBlob), (0, _utils.timeout)(_this.defaultTimeout)]));

              case 1:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }));

      return function (_x5, _x6, _x7, _x8) {
        return _ref2.apply(this, arguments);
      };
    }());
    (0, _defineProperty2["default"])(this, "convert", /*#__PURE__*/function () {
      var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(originBlob, targetType, timeoutValue, progressCallback) {
        return _regenerator["default"].wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                return _context3.abrupt("return", Promise.race([_this.innerConvert(originBlob, targetType, progressCallback), (0, _utils.timeout)(timeoutValue || _this.defaultTimeout)]));

              case 1:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3);
      }));

      return function (_x9, _x10, _x11, _x12) {
        return _ref3.apply(this, arguments);
      };
    }());
    (0, _defineProperty2["default"])(this, "innerConvert", /*#__PURE__*/function () {
      var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(originBlob, originType, progressCallback) {
        var originAb, result, resultArrBuf;
        return _regenerator["default"].wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                _context4.next = 2;
                return (0, _utils.blobToArrayBuffer)(originBlob);

              case 2:
                originAb = _context4.sent;
                _context4.next = 5;
                return (0, _utils.pmToPromiseWithProgress)(_this.worker, (0, _utils.getConvertCommand)(originAb, originType), progressCallback);

              case 5:
                result = _context4.sent;
                resultArrBuf = result.buffer;
                return _context4.abrupt("return", {
                  blob: (0, _utils.audioBufferToBlob)(resultArrBuf),
                  logs: [result.logs]
                });

              case 8:
              case "end":
                return _context4.stop();
            }
          }
        }, _callee4);
      }));

      return function (_x13, _x14, _x15) {
        return _ref4.apply(this, arguments);
      };
    }());
    (0, _defineProperty2["default"])(this, "innerTransformSelf", /*#__PURE__*/function () {
      var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(originBlob) {
        var originAb, result, resultArrBuf;
        return _regenerator["default"].wrap(function _callee5$(_context5) {
          while (1) {
            switch (_context5.prev = _context5.next) {
              case 0:
                _context5.next = 2;
                return (0, _utils.blobToArrayBuffer)(originBlob);

              case 2:
                originAb = _context5.sent;
                _context5.next = 5;
                return (0, _utils.pmToPromise)(_this.worker, (0, _utils.getTransformSelfCommand)(originAb));

              case 5:
                result = _context5.sent;
                resultArrBuf = result.buffer;
                return _context5.abrupt("return", {
                  blob: (0, _utils.audioBufferToBlob)(resultArrBuf),
                  logs: [result.logs]
                });

              case 8:
              case "end":
                return _context5.stop();
            }
          }
        }, _callee5);
      }));

      return function (_x16) {
        return _ref5.apply(this, arguments);
      };
    }());
    (0, _defineProperty2["default"])(this, "transformSelf", /*#__PURE__*/function () {
      var _ref6 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(originBlob) {
        return _regenerator["default"].wrap(function _callee6$(_context6) {
          while (1) {
            switch (_context6.prev = _context6.next) {
              case 0:
                return _context6.abrupt("return", Promise.race([_this.innerTransformSelf(originBlob), (0, _utils.timeout)(_this.defaultTimeout)]));

              case 1:
              case "end":
                return _context6.stop();
            }
          }
        }, _callee6);
      }));

      return function (_x17) {
        return _ref6.apply(this, arguments);
      };
    }());
    (0, _defineProperty2["default"])(this, "clip", /*#__PURE__*/function () {
      var _ref7 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7(originBlob, startSecond, endSecond) {
        return _regenerator["default"].wrap(function _callee7$(_context7) {
          while (1) {
            switch (_context7.prev = _context7.next) {
              case 0:
                return _context7.abrupt("return", Promise.race([_this.innerClip(originBlob, startSecond, endSecond), (0, _utils.timeout)(_this.defaultTimeout)]));

              case 1:
              case "end":
                return _context7.stop();
            }
          }
        }, _callee7);
      }));

      return function (_x18, _x19, _x20) {
        return _ref7.apply(this, arguments);
      };
    }());
    (0, _defineProperty2["default"])(this, "innerClip", /*#__PURE__*/function () {
      var _ref8 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8(originBlob, startSecond, endSecond) {
        var ss, d, originAb, logs, resultArrBuf, result, _result2;

        return _regenerator["default"].wrap(function _callee8$(_context8) {
          while (1) {
            switch (_context8.prev = _context8.next) {
              case 0:
                ss = startSecond;
                d = (0, _lodash.isNumber)(endSecond) ? endSecond - startSecond : _this.end;
                _context8.next = 4;
                return (0, _utils.blobToArrayBuffer)(originBlob);

              case 4:
                originAb = _context8.sent;
                logs = [];

                if (!(d === _this.end)) {
                  _context8.next = 14;
                  break;
                }

                _context8.next = 9;
                return (0, _utils.pmToPromise)(_this.worker, (0, _utils.getClipCommand)(originAb, ss));

              case 9:
                result = _context8.sent;
                resultArrBuf = result.buffer;
                logs.push(result.logs);
                _context8.next = 19;
                break;

              case 14:
                _context8.next = 16;
                return (0, _utils.pmToPromise)(_this.worker, (0, _utils.getClipCommand)(originAb, ss, d));

              case 16:
                _result2 = _context8.sent;
                resultArrBuf = _result2.buffer;
                logs.push(logs);

              case 19:
                return _context8.abrupt("return", {
                  blob: (0, _utils.audioBufferToBlob)(resultArrBuf),
                  logs: logs
                });

              case 20:
              case "end":
                return _context8.stop();
            }
          }
        }, _callee8);
      }));

      return function (_x21, _x22, _x23) {
        return _ref8.apply(this, arguments);
      };
    }());
    (0, _defineProperty2["default"])(this, "innerConcat", /*#__PURE__*/function () {
      var _ref9 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee9(blobs) {
        var arrBufs, i, result, concatBlob;
        return _regenerator["default"].wrap(function _callee9$(_context9) {
          while (1) {
            switch (_context9.prev = _context9.next) {
              case 0:
                arrBufs = [];
                i = 0;

              case 2:
                if (!(i < blobs.length)) {
                  _context9.next = 11;
                  break;
                }

                _context9.t0 = arrBufs;
                _context9.next = 6;
                return (0, _utils.blobToArrayBuffer)(blobs[i]);

              case 6:
                _context9.t1 = _context9.sent;

                _context9.t0.push.call(_context9.t0, _context9.t1);

              case 8:
                i++;
                _context9.next = 2;
                break;

              case 11:
                _context9.t2 = _utils.pmToPromise;
                _context9.t3 = _this.worker;
                _context9.next = 15;
                return (0, _utils.getCombineCommand)(arrBufs);

              case 15:
                _context9.t4 = _context9.sent;
                _context9.next = 18;
                return (0, _context9.t2)(_context9.t3, _context9.t4);

              case 18:
                result = _context9.sent;
                concatBlob = (0, _utils.audioBufferToBlob)(result.buffer);
                return _context9.abrupt("return", {
                  blob: concatBlob,
                  logs: [result.logs]
                });

              case 21:
              case "end":
                return _context9.stop();
            }
          }
        }, _callee9);
      }));

      return function (_x24) {
        return _ref9.apply(this, arguments);
      };
    }());
    (0, _defineProperty2["default"])(this, "clipConvert", /*#__PURE__*/function () {
      var _ref10 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee10(arrayBuffer, originType, startSecond, endSecond, progressCallback) {
        return _regenerator["default"].wrap(function _callee10$(_context10) {
          while (1) {
            switch (_context10.prev = _context10.next) {
              case 0:
                return _context10.abrupt("return", Promise.race([_this.innerClipConvert(arrayBuffer, originType, startSecond, endSecond, progressCallback), (0, _utils.timeout)(_this.defaultTimeout)]));

              case 1:
              case "end":
                return _context10.stop();
            }
          }
        }, _callee10);
      }));

      return function (_x25, _x26, _x27, _x28, _x29) {
        return _ref10.apply(this, arguments);
      };
    }());
    (0, _defineProperty2["default"])(this, "innerClipConvert", /*#__PURE__*/function () {
      var _ref11 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee11(arrayBuffer, originType, startSecond, endSecond, progressCallback) {
        var result, resultArrBuf;
        return _regenerator["default"].wrap(function _callee11$(_context11) {
          while (1) {
            switch (_context11.prev = _context11.next) {
              case 0:
                _context11.next = 2;
                return (0, _utils.pmToPromiseWithProgress)(_this.worker, (0, _utils.getClipConvertCommand)(arrayBuffer, originType, startSecond, endSecond), progressCallback);

              case 2:
                result = _context11.sent;
                resultArrBuf = result.buffer;
                return _context11.abrupt("return", {
                  blob: (0, _utils.audioBufferToBlob)(resultArrBuf),
                  logs: [result.logs]
                });

              case 5:
              case "end":
                return _context11.stop();
            }
          }
        }, _callee11);
      }));

      return function (_x30, _x31, _x32, _x33, _x34) {
        return _ref11.apply(this, arguments);
      };
    }());
    (0, _defineProperty2["default"])(this, "concat", /*#__PURE__*/function () {
      var _ref12 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee12(blobs) {
        return _regenerator["default"].wrap(function _callee12$(_context12) {
          while (1) {
            switch (_context12.prev = _context12.next) {
              case 0:
                return _context12.abrupt("return", Promise.race([_this.innerConcat(blobs), (0, _utils.timeout)(_this.defaultTimeout)]));

              case 1:
              case "end":
                return _context12.stop();
            }
          }
        }, _callee12);
      }));

      return function (_x35) {
        return _ref12.apply(this, arguments);
      };
    }());
    _conf = _conf || {};
    this.defaultTimeout = _conf.timeout || 30 * 1000;
  }

  (0, _createClass2["default"])(Sdk, [{
    key: "toBlob",
    value: function toBlob(audio) {
      return (0, _utils.audioToBlob)(audio);
    }
  }, {
    key: "toAudio",
    value: function toAudio(blob) {
      return (0, _utils.blobToAudio)(blob);
    }
  }, {
    key: "custom",
    value: function custom(config) {
      var inputTimeout = config.timeout;
      return Promise.race([this.innerCustom(config), (0, _utils.timeout)(inputTimeout || this.defaultTimeout)]);
    }
  }, {
    key: "innerCustom",
    value: function () {
      var _innerCustom = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee13(config) {
        var commandLine, audios, progressCallback, MEMFS, audioNames, index, name, audio, arrayBuffer, result;
        return _regenerator["default"].wrap(function _callee13$(_context13) {
          while (1) {
            switch (_context13.prev = _context13.next) {
              case 0:
                commandLine = config.commandLine, audios = config.audios, progressCallback = config.progressCallback;
                MEMFS = [];
                audioNames = Object.keys(audios);
                index = 0;

              case 4:
                if (!(index < audioNames.length)) {
                  _context13.next = 25;
                  break;
                }

                name = audioNames[index];
                audio = audios[name];
                arrayBuffer = void 0;

                if (!(0, _utils.isAudio)(audio)) {
                  _context13.next = 14;
                  break;
                }

                _context13.next = 11;
                return (0, _utils.audioToArrayBuffer)(audio);

              case 11:
                arrayBuffer = _context13.sent;
                _context13.next = 21;
                break;

              case 14:
                if (!(audio instanceof Blob)) {
                  _context13.next = 20;
                  break;
                }

                _context13.next = 17;
                return (0, _utils.blobToArrayBuffer)(audio);

              case 17:
                arrayBuffer = _context13.sent;
                _context13.next = 21;
                break;

              case 20:
                arrayBuffer = audio;

              case 21:
                MEMFS.push({
                  name: name,
                  data: new Uint8Array(arrayBuffer)
                });

              case 22:
                index++;
                _context13.next = 4;
                break;

              case 25:
                _context13.next = 27;
                return (0, _utils.pmToPromiseWithProgress)(this.worker, {
                  type: 'run',
                  arguments: commandLine.split(' '),
                  MEMFS: MEMFS
                }, progressCallback);

              case 27:
                result = _context13.sent;
                return _context13.abrupt("return", {
                  blob: (0, _utils.audioBufferToBlob)(result.buffer),
                  logs: [result.logs]
                });

              case 29:
              case "end":
                return _context13.stop();
            }
          }
        }, _callee13, this);
      }));

      function innerCustom(_x36) {
        return _innerCustom.apply(this, arguments);
      }

      return innerCustom;
    }()
  }]);
  return Sdk;
}();

exports["default"] = Sdk;